#ifndef LOCKER_H_
#define LOCKER_H_
//
#include <windows.h>
//
class ILock {
public:
	virtual void lock()     =0;
	virtual void unlock()   =0;
	virtual bool tryToLock()=0;
};
//
class EventLock :public ILock {
public:
	EventLock();
	~EventLock();
	virtual void lock();
	virtual bool tryToLock();
	virtual void unlock();
private:
	HANDLE _handle;
};
//
class CriticalSectionLock :public ILock {
public:
	CriticalSectionLock();
	~CriticalSectionLock();
	virtual void lock();
	virtual bool tryToLock();
	virtual void unlock();
private:
	CRITICAL_SECTION _cs;
};


class Locker
{
public:
	Locker(ILock& cs) :_cs(cs) {
		_cs.lock();
	}

	~Locker() {
		_cs.unlock();
	}

private:
	ILock& _cs;
};


class TryLocker
{
public:
	TryLocker(ILock& cs) :_cs(cs) {
	}

	~TryLocker() {
		unlock();
	}

	bool tryToLock() {
		return _cs.tryToLock();
	}

	void unlock() {
		_cs.unlock();
	}

private:
	ILock& _cs;
};


#endif //LOCKER_H_